package org.roger.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.analysis.util.CharArraySet;
import org.roger.analyzer.MetaPhoneAnalyzer;
import org.roger.analyzer.PorterStopAnalyzer;
import org.roger.analyzer.SplitSpotAnalyzer;
import org.roger.analyzer.SynonymAnalyzer;
import org.roger.bean.SynonymEngineImpl;
import org.roger.index.LuceneIndex;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * 分析语汇单元
 * @author luoweiyi
 */
public class AnalyzerUtil {
	
	public static void displayTokens(Analyzer analyzer,String text) throws IOException{
		displayTokens(analyzer.tokenStream("", text));
	}
	
	public static void displayTokens(TokenStream stream) throws IOException{
		CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class);
		stream.reset();
		while(stream.incrementToken()){
			System.out.print(cta.toString()+"   ");
		}
		System.out.println();
	}
	
	public static void displayTokenWithFullDetails(Analyzer analyzer,String text) throws IOException{
		TokenStream stream = analyzer.tokenStream("", new StringReader(text));
		stream.reset();
		//语汇单元对应的文本
		CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class);
		//位置增量（默认值为1）
		PositionIncrementAttribute posIncr = stream.addAttribute(PositionIncrementAttribute.class);
		//偏移量
		OffsetAttribute offset = stream.addAttribute(OffsetAttribute.class);
		//类型
		TypeAttribute type = stream.addAttribute(TypeAttribute.class);
		
		KeywordAttribute keywordAttr = stream.addAttribute(KeywordAttribute.class);
		
		int position = 0;
		//遍历所有的语汇单元
		while(stream.incrementToken()){
			int increment = posIncr.getPositionIncrement();
			if(increment > 0){
				position += increment;
				System.out.println();
				System.out.print(position + ":");
			}
			
			System.out.print("["+ cta.toString()+":"+
					offset.startOffset()+"->"+
					offset.endOffset()+"->"+
					type.type()+"]");
			
			System.out.print("Keyword:"+keywordAttr.isKeyword());
			
			System.out.println();
			
			
		}
	}
	
	public static void main(String[] args) {
		try {
//			AnalyzerUtil.displayTokens(new SimpleAnalyzer(), "a b,wo,roger@sina.cn");
		//	AnalyzerUtil.displayTokens(new SplitSpotAnalyzer(), "Its your the freshness of the deep springs of life cat.Its your the freshness of the deep springs of life cat.");
//			AnalyzerUtil.displayTokens(new StopAnalyzer(), "a b,wo,xiao_luo@sina.cn");
//			AnalyzerUtil.displayTokens(new WhitespaceAnalyzer(), "a b,wo,xiao_luo@sina.cn");
//			AnalyzerUtil.displayTokens(new KeywordAnalyzer(), "a b,wo,xiao_luo@sina.cn");
			
//			AnalyzerUtil.displayTokens(new MetaPhoneAnalyzer(), "The b,wo,xiao_luo@sina.cn");
			//AnalyzerUtil.displayTokenWithFullDetails(new Synon(), "I'll E-mail you at xyz@example.com");
			
	//		AnalyzerUtil.displayTokenWithFullDetails(new SynonymAnalyzer(), "Its your the freshness of the deep springs of life cat. I search money to pay.");
			
			//AnalyzerUtil.displayTokenWithFullDetails(new ChineseAnalyzer(), "修改文件编码方式。将鼠标移动到文件上，点击右键，选中“属性”，弹出如图所示对话框，同样是“资源”下有一项“文本文件编码”，选中要使用的编码");
			Analyzer analyzer = new IKAnalyzer(true);
			
			AnalyzerUtil.displayTokenWithFullDetails(analyzer, "修改文件编码方式。将鼠标移动到文件上，点击右键，选中“属性”，弹出如图所示对话框，同样是“资源”下有一项“文本文件编码”，选中要使用的编码");
			
//			List<String> stopWords = new ArrayList<String>();
//			stopWords.add("the");
//			CharArraySet stopWordsSet = StopFilter.makeStopSet(stopWords);
//			AnalyzerUtil.displayTokenWithFullDetails(new SplitSpotAnalyzer(stopWordsSet), "Its your the freshness of the deep springs of life cat.Its your the freshness of the deep springs of life cat.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
